<template>
  <div>
    <!--  搜索表单  -->
    <div>
      <el-input style="width: 240px" placeholder="请输入图书名称" v-model="params.bookName"></el-input>
      <el-input style="width: 240px;margin-left: 10px" placeholder="请输入图书标准码" v-model="params.bookNo"></el-input>
      <el-input style="width: 240px;margin-left: 10px" placeholder="请输入用户名" v-model="params.userName"></el-input>
      <el-button style="margin-left: 5px" type="primary" @click="load">
        <i class="el-icon-search">搜索</i>
      </el-button>
      <el-button style="margin-left: 5px" type="warning" @click="reset">
        <i class="el-icon-refresh">重置</i>
      </el-button>
    </div>

    <el-table :data="tableData" stripe row-key="id" default-expand-all>
      <el-table-column prop="id" label="编号" width="50px"></el-table-column>
      <el-table-column prop="bookName" label="图书名称" width="100px"></el-table-column>
      <el-table-column prop="bookNo" label="标准码" width="100px"></el-table-column>
      <el-table-column prop="userNo" label="用户id" width="90px"></el-table-column>
      <el-table-column prop="userName" label="用户名称" width="80px"></el-table-column>
      <el-table-column prop="userPhone" label="用户电话" width="100px"></el-table-column>
      <el-table-column prop="score" label="图书积分" width="100px"></el-table-column>
      <el-table-column prop="createtime" label="借出时间" width="90px"></el-table-column>
      <el-table-column prop="status" label="借出状态" width="80px"></el-table-column>
      <el-table-column prop="days" label="借出天数" width="70px"></el-table-column>
      <el-table-column prop="returnDate" label="归还日期" width="90px"></el-table-column>
      <el-table-column prop="note" label="过期提醒" width="90px">
        <template v-slot="scope">
          <el-tag type="success" v-if="scope.row.note === '正常状态' ">{{ scope.row.note }}</el-tag>
          <el-tag type="primary" v-if="scope.row.note === '即将过期' ">{{ scope.row.note }}</el-tag>
          <el-tag type="warning" v-if="scope.row.note === '今天到期' ">{{ scope.row.note }}</el-tag>
          <el-tag type="danger" v-if="scope.row.note === '已过期' ">{{ scope.row.note }}</el-tag>
        </template>
      </el-table-column>

      <!--   还书操作   -->
      <el-table-column label="管理" style="width: 400px;">
        <template v-slot="scope">
          <el-button type="primary" @click="returnBooks(scope.row)" v-if="scope.row.status === '已借出' ">还书</el-button>
        </template>
      </el-table-column>

      <!--   数据行操作   -->
      <el-table-column label="操作" style="width: 400px;">
        <template v-slot="scope">
          <template>
            <el-popconfirm style="margin-left: 8px"
                title="您确定删除这行数据吗？"
                @confirm="del(scope.row.id)"
            >
              <el-button type="danger" slot="reference">删除</el-button>
            </el-popconfirm>
          </template>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div style="margin-left: 20px" >
      <el-pagination
          background
          :current-page="params.pageNum"
          :page-sizes="params.pageSize"       #指定每一页有的数据
          layout="prev, pager, next"
          @current-change="handleCurrentChange"
          :total="total">
      </el-pagination>
    </div>

  </div>
</template>


<script>
import request from "@/utils/request";
import Cookies from 'js-cookie'

export default {
  name: 'BorrowList',
  data() {
    return {
      admin: Cookies.get('admin') ? JSON.parse(Cookies.get('admin')) : {},
      tableData:[],
      total:0,
      form : {},
      params: {
        pageNum:1,
        pageSize:10,
        name:'',
      },
    }
  },
  created() {
    this.load()
  },
  methods: {
    load() {
      request.get('/borrow/page',{
        params: this.params
      }).then(res => {
        if (res.code === '200') {
          this.tableData = res.data.list
          this.total = res.data.total
        }
      })
    },
    reset() {
      this.params = {
        pageNum:1,
        pageSize:10,
        bookName:'',
        bookNo:'',
        userName:'',
      }
      this.load()
    },
    handleCurrentChange(pageNum) {
      //点击分页按钮
      // console.log(pageNum)
      this.params.pageNum = pageNum
      this.load()
    },
    del(id) {
      request.delete("borrow/delete/" + id).then(res => {
        if (res.code === '200') {
          this.$notify.success("删除成功")
          this.load()
        }else {
          this.$notify.error(res.msg)
        }
      })
    },
    returnBooks(row) {
      request.post("borrow/saveRetur" , row).then(res => {
        if (res.code === '200') {
          this.$notify.success("还书成功")
          this.load()
        }else {
          this.$notify.error(res.msg)
        }
      })
    }
  }
}
</script>

<style>

</style>